﻿#include "client.h"
#include <QDebug>
#include <QRandomGenerator>
#include <QDataStream>
/*********************************************************************
*     function:回调函数
*  Description:初始化定时器
*        Input:
*       Output:
*       Return:true
*       Others:
*       Auther:lsy
*  Create Time:2023-07-12
*---------------------------------------------------------------------
*  Modify
*  Version       Author        Date            Modification
*  V0.00         lsy           2023-07-12      心跳包15秒一次 CS一致
*
**********************************************************************/
Client::Client(QObject *parent) : QObject(parent)
{
    qDebug()<<"初始化客户端";
    timer_heart=new QTimer(this);  
    timer_reConn=new QTimer(this);
    timer_CAN_1=new QTimer(this);
    socket=new QTcpSocket(this);
    timer_heart->setInterval(1000);//设置定时器间隔5秒  2023年08月03日 更改为5秒
    timer_CAN_1->setInterval(100);//设置定时器间隔100毫秒发送一次
    timer_CAN_1->start();//开始向CAN发送数据

    connect(timer_heart,SIGNAL(timeout()),this,SLOT(doHeart()));//定时器超时发送心跳
    connect(timer_CAN_1,SIGNAL(timeout()),this,SLOT(sendCANdata_1()));//向CAN每隔100ms发送数据   发送一  
    connect(socket,SIGNAL(connected()),this,SLOT(isConnection()));//连接 
    connect(socket,SIGNAL(disconnected()),this,SLOT(isDisConnection()));//断线   
    connect(socket,&QTcpSocket::readyRead,this,&Client::receiveData);
    connect(this,&Client::sig_radar,this,&Client::sendcan_data);
    //timer_CAN_1->start();//开始向CAN发送数据
    connect(&m_Timer, SIGNAL(timeout()), this, SLOT(reConnection()));
    m_Timer.start(1000);

}
/*********************************************************************
*     function:Toconnect
*  Description:TCP客户端连接函数
*        Input:
*       Output:
*       Return:true
*       Others:
*       Auther:lsy
*  Create Time:2023-07-12
*---------------------------------------------------------------------
*  Modify
*  Version       Author        Date            Modification
*  V0.00         lsy           2023-07-12      TCP连接
*
**********************************************************************/
void Client::Toconnect(QString ip, quint16 port)
{
    this->ip=ip;
    this->port=port;
    socket->connectToHost(ip,port);
}
/*********************************************************************
*     function:sendcan_data
*  Description:TCP客户端发送数据
*        Input:
*       Output:
*       Return:true
*       Others:
*       Auther:lsy
*  Create Time:2023-07-12
*---------------------------------------------------------------------
*  Modify
*  Version       Author        Date            Modification
*  V0.00         lsy           2023-07-12      TCP客户端向服务端发送数据
*
**********************************************************************/
void Client::sendcan_data(QString data)
{
    QString start = "55aa";
    QString end = "55AB";
    QString Da = start+data+dw_data+tem_data+end;
    //qDebug()<<Da;
    if(Count == true)
    {
        socket->write(Da.toLatin1());
        socket->flush();       
    }
}
/*********************************************************************
*     function:receiveData
*  Description:TCP客户端接收数据
*        Input:
*       Output:
*       Return:true
*       Others:
*       Auther:lsy
*  Create Time:2023-07-12
*---------------------------------------------------------------------
*  Modify
*  Version       Author        Date            Modification
*  V0.00         lsy           2023-07-12      TCP客户端接收服务端的数据
*
**********************************************************************/
void Client::receiveData()
{
    socket->setReadBufferSize(24); //
    QByteArray rec = socket->readAll();
    QString allData = QString(rec);

    if(allData.startsWith("6688")&&allData.endsWith("7799"))
    {
        //timer_CAN_1->start();//开始向CAN发送数据
        canData = allData.mid(4,16);
        sendCANdata_1();
        //qDebug()<<canData;
    }
}


void Client::run()
{

}
/*********************************************************************
*     function:receiveData
*  Description:TCP客户端向CAN转发数据
**********************************************************************/
//向CAN发送数据
void Client::sendCANdata_1()   
{
    int ID = 0x18D10001;//工控机发送1a
    QString res(canData);
    std::vector<int> vec;
    //QString res(data);
    QByteArray data1 = res.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(res.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit CANCon(ID,vec);

    QString da = "00000000000000000000000000000000";
    emit sig_radar(da);
    //qDebug()<<da;
}

void Client::StopSend()
{
    timer_CAN_1->stop();
}


void Client::doHeart()
{
    qDebug()<<"1s了，发送心跳包";
    QString heart="heartPackage$";
    QByteArray ba=heart.toLocal8Bit();
    socket->write(ba);
}

void Client::isConnection()
{
    qDebug()<<"连接服务器成功";
    emit sig_connect_suc();
    //count=11000;//心跳设置为100(大于10即可)，让重连函数停止
    Count = true;
    connectCount ="1";
    timer_heart->start();//开始心跳
    m_Timer.stop();
}

void Client::isDisConnection()
{
    Count=false;
    connectCount = "0";
    qDebug()<<"*******断开*******";
    emit sig_tcp_cloSuc();
    timer_heart->stop();//断开连接后心跳停止
    m_Timer.start(1000);
}

//重连函数
void Client::reConnection()
{

    qDebug()<<"准备重连...";
    socket->connectToHost(ip,port);
//    count++;
//    if(count<=10000)
//    {
//        qDebug()<<"断开连接,准备重连...(第"<<count<<"次)";
//        socket->connectToHost(ip,port);
//    }
//    else {
//        timer_reConn->stop();//超过10次停止重连
//        count = 0;
//    }

}

void Client::CANdataRec(QString da)
{
    //sendcan = da;
}

void Client::RS485dataRec(QString da)
{
    dw_data = da;
}

void Client::RS485dataTem(QString da)
{
    tem_data = da;
}

void Client::rectem1(QString da)
{
    tem1 = da;
}

void Client::rectem2(QString da)
{
    tem2 = da;
}

void Client::rectem3(QString da)
{
    tem3 = da;
}
